home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Borland Plateform / TURBO PASCAL 1.5 for WIN / OWL.PAK / STDWNDS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-06-08  |  14.3 KB  |  519 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal for Windows                        }
  5. {       Standard windows unit for ObjectWindows         }
  6. {                                                       }
  7. {       Copyright (c) 1991 Borland International        }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit StdWnds;
  12.  
  13. {$R STDWNDS.RES}
  14.  
  15. interface
  16.  
  17. uses WinTypes, WinProcs, WinDos, WObjects, StdDlgs, Strings;
  18.  
  19. type
  20.  
  21.   { TSearchRec }
  22.   TSearchRec = record
  23.     SearchText: array[0..80] of Char;
  24.     CaseSensitive: Bool;
  25.     ReplaceText: array[0..80] of Char;
  26.     ReplaceAll: Bool;
  27.     PromptOnReplace: Bool;
  28.     IsReplace: Boolean;
  29.   end;
  30.  
  31.   { TEditWindow  }
  32.   PEditWindow = ^TEditWindow;
  33.   TEditWindow = object(TWindow)
  34.     Editor: PEdit;
  35.     SearchRec: TSearchRec;
  36.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  37.     constructor Load(var S: TStream);
  38.     procedure Store(var S: TStream);
  39.     procedure WMSize(var Msg: TMessage);
  40.       virtual wm_First + wm_Size;
  41.     procedure WMSetFocus(var Msg: TMessage);
  42.       virtual wm_First + wm_SetFocus;
  43.     procedure CMEditFind(var Msg: TMessage);
  44.       virtual cm_First + cm_EditFind;
  45.     procedure CMEditFindNext(var Msg: TMessage);
  46.       virtual cm_First + cm_EditFindNext;
  47.     procedure CMEditReplace(var Msg: TMessage);
  48.       virtual cm_First + cm_EditReplace;
  49.   private
  50.     procedure DoSearch;
  51.   end;
  52.  
  53.   { TFileWindow }
  54.   PFileWindow = ^TFileWindow;
  55.   TFileWindow = object(TEditWindow)
  56.     FileName: PChar;
  57.     IsNewFile: Boolean;
  58.     constructor Init(AParent: PWindowsObject; ATitle, AFileName: PChar);
  59.     destructor Done; virtual;
  60.     constructor Load(var S: TStream);
  61.     procedure Store(var S: TStream);
  62.     function CanClear: Boolean; virtual;
  63.     function CanClose: Boolean; virtual;
  64.     procedure NewFile;
  65.     procedure Open;
  66.     procedure Read;
  67.     procedure SetFileName(AFileName: PChar);
  68.     procedure ReplaceWith(AFileName: PChar);
  69.     function Save: Boolean;
  70.     function SaveAs: Boolean;
  71.     procedure SetupWindow; virtual;
  72.     procedure Write;
  73.     procedure CMFileNew(var Msg: TMessage);
  74.       virtual cm_First + cm_FileNew;
  75.     procedure CMFileOpen(var Msg: TMessage);
  76.       virtual cm_First + cm_FileOpen;
  77.     procedure CMFileSave(var Msg: TMessage);
  78.       virtual cm_First + cm_FileSave;
  79.     procedure CMFileSaveAs(var Msg: TMessage);
  80.       virtual cm_First + cm_FileSaveAs;
  81.   end;
  82.  
  83. const
  84.   REditWindow: TStreamRec = (
  85.     ObjType: 80;
  86.     VmtLink: Ofs(TypeOf(TEditWindow)^);
  87.     Load:    @TEditWindow.Load;
  88.     Store:   @TEditWindow.Store);
  89.  
  90. const
  91.   RFileWindow: TStreamRec = (
  92.     ObjType: 81;
  93.     VmtLink: Ofs(TypeOf(TFileWindow)^);
  94.     Load:    @TFileWindow.Load;
  95.     Store:   @TFileWindow.Store);
  96.  
  97. procedure RegisterStdWnds;
  98.  
  99. implementation
  100.  
  101. { TSearchDialog }
  102.  
  103. const
  104.   sd_Search          = MakeIntResource($7F10);
  105.   sd_Replace         = MakeIntResource($7F11);
  106.   sd_BCSearch        = MakeIntResource($7F12);
  107.   sd_BCReplace       = MakeIntResource($7F13);
  108.   id_SearchText      = 100;
  109.   id_CaseSensitive   = 101;
  110.   id_ReplaceText     = 102;
  111.   id_ReplaceAll      = 103;
  112.   id_PromptOnReplace = 104;
  113.  
  114. type
  115.   PSearchDialog = ^TSearchDialog;
  116.   TSearchDialog = object(TDialog)
  117.     constructor Init(AParent: PWindowsObject; Template: PChar;
  118.       var SearchRec: TSearchRec);
  119.   end;
  120.  
  121. constructor TSearchDialog.Init(AParent: PWindowsObject; Template: PChar;
  122.   var SearchRec: TSearchRec);
  123. var
  124.   C: PWindowsObject;
  125. begin
  126.   TDialog.Init(AParent, Template);
  127.   C := New(PEdit, InitResource(@Self, id_SearchText,
  128.     SizeOf(SearchRec.SearchText)));
  129.   C := New(PCheckBox, InitResource(@Self, id_CaseSensitive));
  130.   if Template = sd_Replace then
  131.   begin
  132.     C := New(PEdit, InitResource(@Self, id_ReplaceText,
  133.       SizeOf(SearchRec.ReplaceText)));
  134.     C := New(PCheckBox, InitResource(@Self, id_ReplaceAll));
  135.     C := New(PCheckBox, InitResource(@Self, id_PromptOnReplace));
  136.   end;
  137.   TransferBuffer := @SearchRec;
  138. end;
  139.  
  140. { TEditWindow }
  141.  
  142. { Constructor for a TEditWindow.  Initializes its data fields using passed
  143.   parameters and default values.  Constructs its child edit control. }
  144. constructor TEditWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  145. begin
  146.   TWindow.Init(AParent, ATitle);
  147.   Editor := New(PEdit, Init(@Self, 200, nil, 0, 0, 0, 0, 0, True));
  148.   with Editor^.Attr do
  149.     Style := Style or es_NoHideSel;
  150.   FillChar(SearchRec, SizeOf(SearchRec), #0);
  151. end;
  152.  
  153. { Load a TEditWindow from the given stream }
  154. constructor TEditWindow.Load(var S: TStream);
  155. begin
  156.   TWindow.Load(S);
  157.   GetChildPtr(S, Editor);
  158. end;
  159.  
  160. { Store a TEditWindow to the given stream }
  161. procedure TEditWindow.Store(var S: TStream);
  162. begin
  163.   TWindow.Store(S);
  164.   PutChildPtr(S, Editor);
  165. end;
  166.  
  167. { Responds to an incoming wm_Size message by resizing the child edit
  168.   control according to the size of the TEditWindow's client area. }
  169. procedure TEditWindow.WMSize(var Msg: TMessage);
  170. begin
  171.   TWindow.WMSize(Msg);
  172.   SetWindowPos(Editor^.HWindow, 0, -1, -1, Msg.LParamLo+2, Msg.LParamHi+2,
  173.     swp_NoZOrder);
  174. end;
  175.  
  176. { Responds to an incoming wm_SetFocus message by setting the focus to the
  177.   child edit control. }
  178. procedure TEditWindow.WMSetFocus(var Msg: TMessage);
  179. begin
  180.   SetFocus(Editor^.HWindow);
  181. end;
  182.  
  183. procedure TEditWindow.DoSearch;
  184. var
  185.   S: array[0..80] of Char;
  186.   P: Pointer;
  187.   Rslt: Integer;
  188. begin
  189.   Rslt := 0;
  190.   with SearchRec do
  191.     repeat
  192.       Rslt := Editor^.Search(-1, SearchText, CaseSensitive);
  193.       if Rslt = -1 then
  194.       begin
  195.         if not IsReplace or not ReplaceAll then
  196.         begin
  197.           P := @SearchText;
  198.           WVSPrintF(S, '"%0.60s" not found.', P);
  199.           MessageBox(HWindow, S, 'Find error', mb_OK + mb_IconExclamation);
  200.         end;
  201.       end
  202.       else
  203.         if IsReplace then
  204.           if not PromptOnReplace then Editor^.Insert(ReplaceText)
  205.           else
  206.           begin
  207.             Rslt := MessageBox(HWindow, 'Replace this occurrence?',
  208.               'Search/Replace', mb_YesNoCancel + mb_IconQuestion);
  209.             if Rslt = id_Yes then Editor^.Insert(ReplaceText)
  210.             else if Rslt = id_Cancel then Exit;
  211.           end;
  212.     until (Rslt = -1) or not ReplaceAll or not IsReplace;
  213. end;
  214.  
  215. procedure TEditWindow.CMEditFind(var Msg: TMessage);
  216. var
  217.   Dialog: PChar;
  218. begin
  219.   if BWCCClassNames then
  220.     Dialog := sd_BCSearch
  221.   else
  222.     Dialog := sd_Search;
  223.   if Application^.ExecDialog(New(PSearchDialog, Init(@Self,
  224.     Dialog, SearchRec))) = id_OK then
  225.   begin
  226.     SearchRec.IsReplace := False;
  227.     DoSearch;
  228.   end;
  229. end;
  230.  
  231. procedure TEditWindow.CMEditFindNext(var Msg: TMessage);
  232. begin
  233.   DoSearch;
  234. end;
  235.  
  236. procedure TEditWindow.CMEditReplace(var Msg: TMessage);
  237. var
  238.   Dialog: PChar;
  239. begin
  240.   if BWCCClassNames then
  241.     Dialog := sd_BCReplace
  242.   else
  243.     Dialog := sd_Replace;
  244.   if Application^.ExecDialog(New(PSearchDialog, Init(@Self,
  245.     Dialog, SearchRec))) = id_OK then
  246.   begin
  247.     SearchRec.IsReplace := True;
  248.     DoSearch;
  249.   end;
  250. end;
  251.  
  252. { TFileWindow }
  253.  
  254. { Constructor for a TFileWindow.  Initializes its data fields using
  255.   passed parameters and default values. }
  256. constructor TFileWindow.Init(AParent: PWindowsObject; ATitle,
  257.   AFileName: PChar);
  258. begin
  259.   TEditWindow.Init(AParent, ATitle);
  260.   IsNewFile := True;
  261.   FileName := StrNew(AFileName);
  262. end;
  263.  
  264. { Dispose of the file name }
  265. destructor TFileWindow.Done;
  266. begin
  267.   StrDispose(FileName);
  268.   TEditWindow.Done;
  269. end;
  270.  
  271. { Load a TFileWindow from the stream }
  272. constructor TFileWindow.Load(var S: TStream);
  273. begin
  274.   TEditWindow.Load(S);
  275.   FileName := S.StrRead;
  276.   IsNewFile := FileName = nil;
  277. end;
  278.  
  279. { Store a TFileWindow from the stream }
  280. procedure TFileWindow.Store(var S: TStream);
  281. begin
  282.   TEditWindow.Store(S);
  283.   S.StrWrite(FileName);
  284. end;
  285.  
  286. { Performs setup for a TFileWindow, appending 'Untitled' to its caption }
  287. procedure TFileWindow.SetupWindow;
  288. begin
  289.   TEditWindow.SetupWindow;
  290.   SetFileName(FileName);
  291.   if FileName <> nil then Read;
  292. end;
  293.  
  294. { Sets the file name of the window and updates the caption.  Assumes
  295.   that the AFileName parameter and the FileName instance variable were
  296.   allocated by StrNew. }
  297. procedure TFileWindow.SetFileName(AFileName: PChar);
  298. var
  299.   NewCaption: array[0..80] of Char;
  300.   P: array[0..1] of PChar;
  301. begin
  302.   if FileName <> AFileName then
  303.   begin
  304.     StrDispose(FileName);
  305.     FileName := StrNew(AFileName);
  306.   end;
  307.   P[0] := Attr.Title;
  308.   if FileName = nil then P[1] := '(Untitled)'
  309.   else P[1] := AFileName;
  310.   if Attr.Title = nil then SetWindowText(HWindow, P[1])
  311.   else
  312.   begin
  313.     WVSPrintF(NewCaption, '%0.40s - %0.37s', P[0]);
  314.     SetWindowText(HWindow, NewCaption);
  315.   end;
  316. end;
  317.  
  318. { Begins the edit of a new file, after determining that it is Ok to
  319.   clear the TEdit's text. }
  320. procedure TFileWindow.NewFile;
  321. begin
  322.   if CanClear then
  323.   begin
  324.     Editor^.Clear;
  325.     InvalidateRect(Editor^.HWindow, nil, False);
  326.     Editor^.ClearModify;
  327.     IsNewFile := True;
  328.     SetFileName(nil);
  329.   end;
  330. end;
  331.  
  332. { Replaces the current file with the given file. }
  333. procedure TFileWindow.ReplaceWith(AFileName: PChar);
  334. begin
  335.   SetFileName(AFileName);
  336.   Read;
  337.   InvalidateRect(Editor^.HWindow, nil, False);
  338. end;
  339.  
  340. { Brings up a dialog allowing the user to open a file into this
  341.   window.  Save as selecting File|Open from the menus. }
  342. procedure TFileWindow.Open;
  343. var
  344.   TmpName: array[0..fsPathName] of Char;
  345. begin
  346.   if CanClear and (Application^.ExecDialog(New(PFileDialog,
  347.      Init(@Self, PChar(sd_FileOpen), StrCopy(TmpName, '*.*')))) = id_Ok) then
  348.     ReplaceWith(TmpName);
  349. end;
  350.  
  351. { Reads the contents of a previously-specified file into the TEdit
  352.   child control. }
  353. procedure TFileWindow.Read;
  354. const
  355.   BufferSize = 1024;
  356. var
  357.   CharsToRead: LongInt;
  358.   BlockSize: Integer;
  359.   AStream: PDosStream;
  360.   ABuffer: PChar;
  361. begin
  362.   AStream := New(PDosStream, Init(FileName, stOpen));
  363.   ABuffer := MemAlloc(BufferSize + 1);
  364.   CharsToRead := AStream^.GetSize;
  365.   if ABuffer <> nil then
  366.   begin
  367.     Editor^.Clear;
  368.     while CharsToRead > 0 do
  369.     begin
  370.       if CharsToRead > BufferSize then
  371.         BlockSize := BufferSize
  372.       else BlockSize := CharsToRead;
  373.       AStream^.Read(ABuffer^, BlockSize);
  374.       ABuffer[BlockSize] := Char(0);
  375.       Editor^.Insert(ABuffer);
  376.       CharsToRead := CharsToRead - BlockSize;
  377.     end;
  378.     IsNewFile := False;
  379.     Editor^.ClearModify;
  380.     Editor^.SetSelection(0, 0);
  381.     FreeMem(ABuffer, BufferSize + 1);
  382.   end;
  383.   Dispose(AStream, Done);
  384. end;
  385.  
  386. { Saves the contents of the TEdit child control into the file currently
  387.   being editted.  Returns true if the file was saved. }
  388. function TFileWindow.Save: Boolean;
  389. begin
  390.   Save := True;
  391.   if Editor^.IsModified then
  392.     if IsNewFile then Save := SaveAs
  393.     else Write;
  394. end;
  395.  
  396. { Saves the contents of the TEdit child control into a file whose name
  397.   is retrieved from the user, through execution of a "Save" file
  398.   dialog.  Returns true if the file was saved. }
  399. function TFileWindow.SaveAs: Boolean;
  400. var
  401.   TmpName: array[0..fsPathName] of Char;
  402. begin
  403.   SaveAs := False;
  404.   if FileName <> nil then StrCopy(TmpName, FileName)
  405.   else TmpName[0] := #0;
  406.   if Application^.ExecDialog(New(PFileDialog,
  407.     Init(@Self, PChar(sd_FileSave), TmpName))) = id_Ok then
  408.   begin
  409.     SetFileName(TmpName);
  410.     Write;
  411.     SaveAs := True;
  412.   end;
  413. end;
  414.  
  415. { Writes the contents of the TEdit child control to a previously-specified
  416.   file.  If the operation will cause truncation of the text, first confirms
  417.   (through displaying a message box) that it is OK to proceed. }
  418. procedure TFileWindow.Write;
  419. const
  420.   BufferSize = 1024;
  421. var
  422.   CharsToWrite, CharsWritten: LongInt;
  423.   BlockSize: Integer;
  424.   AStream: PDosStream;
  425.   ABuffer: pointer;
  426.   NumLines: Integer;
  427. begin
  428.   NumLines := Editor^.GetNumLines;
  429.   CharsToWrite := Editor^.GetLineIndex(NumLines-1) +
  430.     Editor^.GetLineLength(NumLines-1);
  431.   AStream := New(PDosStream, Init(FileName, stCreate));
  432.   ABuffer := MemAlloc(BufferSize + 1);
  433.   CharsWritten := 0;
  434.   if ABuffer <> nil then
  435.   begin
  436.     while CharsWritten < CharsToWrite do
  437.     begin
  438.       if CharsToWrite - CharsWritten > BufferSize then
  439.         BlockSize := BufferSize
  440.       else BlockSize := CharsToWrite - CharsWritten;
  441.       Editor^.GetSubText(ABuffer, CharsWritten, CharsWritten + BlockSize);
  442.       AStream^.Write(ABuffer^, BlockSize);
  443.       CharsWritten := CharsWritten + BlockSize;
  444.     end;
  445.     IsNewFile := False;
  446.     Editor^.ClearModify;
  447.     FreeMem(ABuffer, BufferSize + 1);
  448.   end;
  449.   Dispose(AStream, Done);
  450. end;
  451.  
  452. { Returns a Boolean value indicating whether or not it is Ok to clear
  453.   the TEdit's text.  Returns True if the text has not been changed, or
  454.   if the user Oks the clearing of the text. }
  455. function TFileWindow.CanClear: Boolean;
  456. var
  457.   S: array[0..fsPathName+27] of Char;
  458.   P: PChar;
  459.   Rslt: Integer;
  460. begin
  461.   CanClear := True;
  462.   if Editor^.IsModified then
  463.   begin
  464.     if FileName = nil then StrCopy(S, 'Untitled file has changed. Save?')
  465.     else
  466.     begin
  467.       P := FileName;
  468.       WVSPrintF(S, 'File "%s" has changed.  Save?', P);
  469.     end;
  470.     Rslt := MessageBox(HWindow, S, 'File Changed', mb_YesNoCancel or
  471.       mb_IconQuestion);
  472.     if Rslt = id_Yes then CanClear := Save
  473.     else CanClear := Rslt <> id_Cancel;
  474.   end;
  475. end;
  476.  
  477. { Returns a Boolean value indicating whether or not it is Ok to close
  478.   the TEdit's text.  Returns the result of a call to Self.CanClear. }
  479. function TFileWindow.CanClose: Boolean;
  480. begin
  481.   CanClose := CanClear;
  482. end;
  483.  
  484. { Responds to an incoming "New" command (with a cm_FileNew command
  485.   identifier) by calling Self.New. }
  486. procedure TFileWindow.CMFileNew(var Msg: TMessage);
  487. begin
  488.   NewFile;
  489. end;
  490.  
  491. { Responds to an incoming "Open" command (with a cm_FileOpen command
  492.   identifier) by calling Self.Open. }
  493. procedure TFileWindow.CMFileOpen(var Msg: TMessage);
  494. begin
  495.   Open;
  496. end;
  497.  
  498. { Responds to an incoming "Save" command (with a cm_FileSave command
  499.   identifier) by calling Self.Save. }
  500. procedure TFileWindow.CMFileSave(var Msg: TMessage);
  501. begin
  502.   Save;
  503. end;
  504.  
  505. { Responds to an incoming "SaveAs" command (with a cm_FileSaveAs command
  506.   identifier) by calling Self.SaveAs. }
  507. procedure TFileWindow.CMFileSaveAs(var Msg: TMessage);
  508. begin
  509.   SaveAs;
  510. end;
  511.  
  512. procedure RegisterStdWnds;
  513. begin
  514.   RegisterType(REditWindow);
  515.   RegisterType(RFileWindow);
  516. end;
  517.  
  518. end.
  519.